package c0907m.group2.aitsm.core.db;

import c0907m.group2.aitsm.util.Helper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Author: LongDH
 * Date: 4/27/11
 * Time: 2:56 PM
 * <p/>
 * Base class for dealing with database's entities.
 */
public abstract class BaseDb {
    protected Connection con;
    protected PreparedStatement st;
    protected ResultSet rs;

    /**
     * Connect to database.
     *
     * @throws SQLException if a database access error occurs.
     */
    public void connect() throws SQLException {
        close();
        con = Helper.getConnection();
    }

    /**
     * Wipe out all connections.
     *
     * @throws SQLException if a database access error occurs.
     */
    public void close() throws SQLException {
        if (con != null) con.close();
        if (st != null) st.close();
        if (rs != null) rs.close();
    }
}
